
package com.jgp.glmeeting.ema.util;

import com.jgp.glmeeting.ema.server.jwsserver.sms.ISmsOperator;
import com.jgp.glmeeting.ema.server.jwsserver.sms.MtMessage;
import com.jgp.glmeeting.ema.server.jwsserver.sms.MtMessageRes;
import com.jgp.glmeeting.ema.server.jwsserver.sms.SmsOperatorImpServiceLocator;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.service.spi.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.rmi.RemoteException;

public final class EmaClient {


	private String username = "gl96660";
	private String password = "lz96660";
	private String smsType = "sdjstSmstype"; // "12"
	private boolean isUsing = true;

	private static EmaClient instance = new EmaClient();

	private EmaClient() {
	}

	public static EmaClient getInstance() {
		if (instance == null) {
			instance = new EmaClient();
		}
		return instance;
	}

	/**
	 * 发送短消息
	 * 
	 * @param msg MtMessage对象
	 * @return MtMessageRes对象
	 * @throws RemoteException 
	 * @throws ServiceException 
	 */
	public MtMessageRes sendSms(MtMessage msg) throws RemoteException, ServiceException, javax.xml.rpc.ServiceException {
		if (msg == null) {
			throw new NullPointerException("MtMessage cannot be null.");
		}
		if (StringUtils.isBlank(msg.getContent())) {
			throw new NullPointerException("message content cannot be null.");
		}
		if (msg.getPhoneNumber() == null && msg.getPhoneNumber().length <= 0) {
			throw new NullPointerException("phoneNumber cannot be null.");
		}

		MtMessageRes msgRes = new MtMessageRes();
		if (isUsing) {
			SmsOperatorImpServiceLocator service = new SmsOperatorImpServiceLocator();
			ISmsOperator operator;
			operator = service.getSmsOperatorImpPort();
			msgRes = operator.sendSms(username, MD5.MD5Encode(password), smsType, msg);
			return msgRes;
		} else {
			//logger.warn("sdjstIsUsing is false, so no sms is sent out.");
			msgRes.setSubStat(EmaConstant.STATE_P999);
			msgRes.setSubStatDes(EmaConstant.STATE_P999_DES);
			return msgRes;
		}
	}
}
